package com.zara.store.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class IntegrationTransformer {
	
	public String transform(String in) throws TransformerException {
		LOG2.addLine(in);
		final StringReader inReader = new StringReader(in);
		final StreamSource inSource = new StreamSource(inReader);
		final StringWriter outWriter = new StringWriter();
		final StreamResult outResult = new StreamResult(outWriter);
		transformer.transform(inSource, outResult);
		String out = outWriter.toString();
		LOG2.addLine(out);
		return out;
	}

	public IntegrationTransformer() throws IOException,
			TransformerException {
		InputStream templateStream = null;
		try {
			templateStream = openTemplateStream();
			final StreamSource templateSource = new StreamSource(templateStream);
			final TransformerFactory tf = TransformerFactory.newInstance();
			templates = tf.newTemplates(templateSource);
			transformer = templates.newTransformer();
		} finally {
			if (templateStream != null) {
				templateStream.close();
			}
		}
	}

	private InputStream openTemplateStream() {
		//return getClass().getResourceAsStream(templateFilename);
		try {
			return new FileInputStream(XSL);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	private Templates templates;

	private Transformer transformer;

	private static final String XSL = "c:/zara-workspace/integration.xsl";
	private final static FileHelper LOG2 = new FileHelper("c:/zara-workspace/zara.integration.log");
	
	public static final String SAMPLE_OFAD = "<ofad>"
			+ "<fechaalta class='sql-timestamp'>2008-11-19 22:21:50.125</fechaalta>"
			+ "<articulos>"
			+ "<articulo><tipo>Ropa</tipo><referencia>467</referencia><linea>Basic</linea><descripcion>lalala</descripcion><color>Blanco</color><talle>40</talle><seccion>Mujer</seccion><precio>33.0</precio><origen>Argentina</origen>"
			+ "<centrosdistribucion><codigo>2</codigo><codigo>1</codigo></centrosdistribucion>"
			+ "<preciooferta>0</preciooferta><fechaalta class='sql-timestamp'>2008-11-19 22:21:50.921</fechaalta><nombre></nombre><composicion></composicion><categoria></categoria><medidas></medidas></articulo>"
			+ "<articulo><tipo>Hogar</tipo><referencia>3433333333</referencia><linea>Unica</linea><nombre>fdf</nombre><descripcion>dsdsd</descripcion><medidas>0.0x34.0x34.00</medidas><color>Unico</color><seccion>Alfombra</seccion><precio>2333.0</precio><categoria>Cama</categoria><composicion>Piel Bovina: 30.0</composicion><centrosdistribucion><codigo>1</codigo></centrosdistribucion><talle></talle><origen></origen><preciooferta>0</preciooferta><fechaalta class='sql-timestamp'>2008-11-19 22:21:51.218</fechaalta></articulo>"
			+ "</articulos><ofadid>0</ofadid></ofad>";

	public static final String SAMPLE_ENVT = "<envt>"
			+ "<fechaalta class=\"sql-timestamp\">2008-11-19 22:04:03.593</fechaalta>"
			+ "<items>" + "<envtitem>" + "<referencia>3333333333</referencia>"
			+ "<cantidad>5</cantidad>" + "</envtitem>" + "<envtitem>"
			+ "<referencia>2222222222</referencia>" + "<cantidad>10</cantidad>"
			+ "</envtitem>" + "<envtitem>"
			+ "<referencia>1111111111</referencia>" + "<cantidad>15</cantidad>"
			+ "</envtitem>" + "</items>" + "</envt>";

	private static final String SAMPLE_PALC = "<palc>" + "<id>1</id>"
			+ "<date>2008-06-26 21:06:12.906 ART</date>" + "<items>"
			+ "<product>" + "<productId>0</productId>" + "<amount>10</amount>"
			+ "<barcode>1111111111</barcode>" + "</product>" + "<product>"
			+ "<productId>0</productId>" + "<amount>10</amount>"
			+ "<barcode>2222222222</barcode>" + "</product>" + "</items>"
			+ "</palc>";
	
	public static final String SAMPLE_OFAD2 = "<ofad><fechaalta class='sql-timestamp'>2008-11-20 16:15:14.556</fechaalta><articulos><articulo><tipo>Ropa</tipo><referencia>3</referencia><linea>Basic</linea><descripcion>Camisa</descripcion><color>Blanco</color><talle>50</talle><seccion>Mujer</seccion><precio>34.0</precio><origen>Argentina</origen><centrosdistribucion><codigo>2</codigo></centrosdistribucion><preciooferta>0</preciooferta><fechaalta class='sql-timestamp'>2008-11-20 16:15:14.666</fechaalta><nombre></nombre><composicion></composicion><categoria></categoria><medidas></medidas></articulo></articulos><ofadid>0</ofadid></ofad>";

	public static void main(String[] args) throws Exception {
		IntegrationTransformer integrationTransformer = new IntegrationTransformer();
		String ofadXml = integrationTransformer.transform(SAMPLE_OFAD);
		String ofadXml2 = integrationTransformer.transform(SAMPLE_OFAD2);
		String envtXml = integrationTransformer.transform(SAMPLE_ENVT);
		System.out.println(ofadXml);
		System.out.println(envtXml);
		System.out.println(integrationTransformer.transform(SAMPLE_PALC));
		EnvtVO envtVO = new EnvtHelper().createEnvtFromXml(envtXml);
		System.out.println(envtVO);
		OfadVO ofadVO = new OfadHelper().createOfadFromXml(ofadXml);
		System.out.println(ofadVO);
		OfadVO ofadVO2 = new OfadHelper().createOfadFromXml(ofadXml2);
		System.out.println(ofadVO2);

	}

}
